﻿using Newtonsoft.Json.Linq;
using System;
using System.IO;

namespace UpdateHotel
{
    class Program
    {
        public static string CONFIGJSON = "config.json";

        static void Main(string[] args)
        {
            Console.WriteLine("开始检查更新！");
            string path = Environment.GetEnvironmentVariable("systemdrive");

            string dic = path + "\\Program Files (x86)\\greencloud\\oHotelWeb\\"; //Directory.GetCurrentDirectory();
            string dicResource = dic + "electronConfig";
            string dicConfig = dic+ "resources\\app\\" + "public\\mainjs\\";

            JObject electronJson = readElectronConfigJson(dicResource);

            JObject configJson = readConfigJson(dicConfig + CONFIGJSON);
            HttpDldFile df = new HttpDldFile();

            string url = configJson["http_type"].ToString() + "://" + electronJson["ip"].ToString() + "/" + electronJson["name"].ToString() + "/mainjs/";
            JObject urlJson = readUrlJson(url + CONFIGJSON);

            if (urlJson == null)
            {
                Console.WriteLine("版本有误！请联系管理员。按任意键结束程序。");
                Console.ReadLine();
                return;
            }
            else if (urlJson["version"].ToString() == configJson["version"].ToString())
            {
                Console.WriteLine("版本一致无需更新！按任意键结束程序。");
                Console.ReadLine();
                return;
            }
            Console.WriteLine("开始下载更新！");
            JArray fileList = JArray.Parse(urlJson["file_list"].ToString());

            foreach (JToken jk in fileList)
            {
                string fileName = jk.ToString();
                string urlFile = url + fileName;

                Console.WriteLine(urlFile + "开始下载！");
                df.Download(urlFile, dicConfig + "\\" + fileName);
                Console.WriteLine(urlFile + "下载完成！");
            }
            Console.WriteLine("更新成功！按任意键结束程序。");
            Console.ReadLine();
        }


        public static JObject readUrlJson(string url)
        {
            Console.WriteLine("获取服务器config配置文件！");
            JObject urlJson = null;
            try
            {
                string getUrlJson = HttpUtils.Get(url);
                urlJson = JObject.Parse(getUrlJson);
            }
            catch (Exception e)
            {
                Console.WriteLine(url + "的json存在问题！请检查");
            }
            return urlJson;
        }


        /// <summary>
        /// 读取config.json
        /// </summary>
        /// <returns></returns>
        public static JObject readConfigJson(string dicConfig)
        {
            JObject jObject;
            Console.WriteLine("获取本地config配置文件！");
            StreamReader sr = new StreamReader(dicConfig);
            string text = null;
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                text += str;
            }
            sr.Close();

            try
            {
                jObject = JObject.Parse(text);

            }
            catch (Exception e)
            {
                jObject = new JObject();
                jObject.Add("version", "1.0.0");
                jObject.Add("http_type", "http");
                jObject.Add("name", "http");
            }
            return jObject;
        }

        /// <summary>
        /// 读取electronConfig
        /// </summary>
        /// <returns></returns>
        public static JObject readElectronConfigJson(string resource)
        {
            JObject jObject;
            Console.WriteLine("获取本地Electron配置文件！");
            StreamReader sr = new StreamReader(resource);
            string text = null;
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                text += str;
            }
            sr.Close();

            try
            {
                jObject = JObject.Parse(text);

            }
            catch (Exception e)
            {
                jObject = new JObject();
                jObject.Add("ip", text);
            }
            return jObject;
        }
    }
}
